package lib.quasar.widget.edit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import lib.quasar.context.BaseConstant;

import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;

/**
 * description: 当前类描述信息
 * created by kalu on 2018/4/24 21:11
 */
@SuppressLint("AppCompatCustomView")
public class CheckEditText extends EditText implements TextWatcher, View.OnFocusChangeListener, ViewTreeObserver.OnScrollChangedListener {

    private float maxInput, minInput = 0f;
    private int dotLength = -1;
    private int mColorWarning = Color.RED;
    private int mColorNormal = Color.BLACK;

    public CheckEditText(Context context) {
        super(context);
    }

    public CheckEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = null;

        try {

            array = context.obtainStyledAttributes(attrs, R.styleable.CheckEditText);
            maxInput = array.getFloat(R.styleable.CheckEditText_cet_max, maxInput);
            minInput = array.getFloat(R.styleable.CheckEditText_cet_min, minInput);
            dotLength = array.getInt(R.styleable.CheckEditText_cet_dot_length, dotLength);
            mColorWarning = array.getColor(R.styleable.CheckEditText_cet_text_color_warning, mColorWarning);
            mColorNormal = array.getColor(R.styleable.CheckEditText_cet_text_color_normal, mColorNormal);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        } finally {
            if (null == array) return;
            array.recycle();
        }
    }

    public CheckEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = null;

        try {

            array = context.obtainStyledAttributes(attrs, R.styleable.CheckEditText);
            maxInput = array.getFloat(R.styleable.CheckEditText_cet_max, maxInput);
            minInput = array.getFloat(R.styleable.CheckEditText_cet_min, minInput);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (null == array) return;
            array.recycle();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        setOnFocusChangeListener(null);
        removeTextChangedListener(this);
        getViewTreeObserver().removeOnScrollChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {

        if (minInput == maxInput) return;

        String trim = s.toString().trim();
        if (TextUtils.isEmpty(trim)) return;

        // 存在小数点
        if (dotLength != -1 && !TextUtils.isEmpty(trim) && trim.length() > 0 && trim.contains(BaseConstant.DOT)) {

            int dot = trim.indexOf(BaseConstant.DOT);
            int length = trim.length() - dot - 1;

            if (length > dotLength) {

                int temp = (length - dotLength);
                trim = trim.substring(0, trim.length() - temp);
                setText(trim);
                setSelection(trim.length());
            }
        }

        try {
            final float curInput = Float.parseFloat(trim);

            if (curInput < minInput) {
                setTextColor(mColorWarning);
                if (null != mOnInputChangeListener) {
                    mOnInputChangeListener.onInputChange(trim, hasFocus(), false, true);
                }
            } else if (curInput > maxInput) {
                setTextColor(mColorWarning);
                if (null != mOnInputChangeListener) {
                    mOnInputChangeListener.onInputChange(trim, hasFocus(), false, true);
                }
            } else {
                setTextColor(mColorNormal);
                if (null != mOnInputChangeListener) {
                    mOnInputChangeListener.onInputChange(trim, hasFocus(), false, false);
                }
            }
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
            if (null != mOnInputChangeListener) {
                mOnInputChangeListener.onInputChange(trim, hasFocus(), false, false);
            }
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (null == mOnInputChangeListener) return;
        final String inputs = getText().toString().trim();
        if (TextUtils.isEmpty(inputs)) return;
        mOnInputChangeListener.onInputChange(inputs, hasFocus(), false, false);
    }

    @Override
    public void onScrollChanged() {
        if (null == mOnInputChangeListener) return;
        final String inputs = getText().toString().trim();
        if (TextUtils.isEmpty(inputs)) return;
        mOnInputChangeListener.onInputChange(inputs, hasFocus(), true, false);
    }

    /*********************************************************************************************/

    public void openViewTreeObserverListeren() {
        getViewTreeObserver().addOnScrollChangedListener(this);
    }

    public float getMin() {
        return minInput;
    }

    public float getMax() {
        return maxInput;
    }

    public void setMax(float max) {
        this.maxInput = max;
    }

    public void setMin(float min) {
        this.minInput = min;
    }

    public void setDotLength(int length) {
        this.dotLength = length;
    }

    public void setMaxLength(int length) {
        super.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)}); //最大输入长度
    }

    public void setInputType(int type) {
        super.setInputType(type); //输入类型
    }

    /*********************************************************************************************/

    private OnInputChangeListener mOnInputChangeListener;

    public interface OnInputChangeListener {
        void onInputChange(String input, boolean focus, boolean scroll, boolean waining);
    }

    public void setOnInputChangeListener(OnInputChangeListener listener) {
        this.mOnInputChangeListener = listener;
    }
}
